/**
 * Created by easezhi on 2015/1/26.
 */
$('#serviceType').datagrid({
    url:'/lifeService/serviceType/list',
    method:'post',
    //border : false,
    rownumbers: true,
    idField : 'serv_id',
    pagination : true,
    sortName : 'serv_type',
    pageSize : 10,
    pageList : [ 10, 20, 40, 80 ],
    fitColumns : true,
    fit : true,
    checkOnSelect : false,
    selectOnCheck : true,
    striped: true,//条纹交替
    columns:[[
        {field:'',checkbox:true},
        {field:'serv_name',title:'服务名称',width:100,align:'center'},
        {field:'serv_type',title:'服务类型',width:100,align:'center',sortable:true},
        {field:'describe',title:'描述信息',width:200,align:'center'}
    ]],
    toolbar : '#sttb'
});



function delst(){
    var rows = $('#serviceType').datagrid('getChecked');
    if (rows.length > 0) {
        var ids = [];
        for ( var i = 0; i < rows.length; i++) {
            ids.push(rows[i].serv_id);
        }
        ids = ids.join();
        $.ajax({
            url:'/lifeService/serviceType/predel',
            data:{ids:ids},
            type:'post',
            timeout:6000,
            success:function(data, status){
                if(!data.err){
                    data = eval('(' + data + ')');
                    if(data.count == 0){
                        $.messager.confirm('确认', '是否删除所选项目？', function(b) {
                            if (b) {
                                $.ajax({
                                    url : '/auth/lifeService/serviceType/del',
                                    data : {
                                        ids : ids
                                    },
                                    type:'POST',
                                    success : function(data) {
                                        data = eval('('+data+')');
                                        if(data.count > 0){
                                            $('#serviceType').datagrid('reload');
                                            $('#serviceType').datagrid('uncheckAll');// 取消选中的所有记录
                                            showMsg('删除成功！');
                                        }else{
                                            showMsg('删除失败！');
                                        }
                                    }
                                });
                            }
                        });
                    }else{
                        $.messager.alert('提示', '该分类下有商品信息，暂时无法删除', 'error');
                    }
                }else{
                    showMsg('网络有误，请稍候再试')
                }
            },
            error:function(){
                showMsg('网络有误，请稍候再试')
            }
        })


    } else {
        $.messager.alert('提示', '请勾选要删除的商家。', 'error');
    }
}

function editst(){
    var rows = $('#serviceType').datagrid('getChecked');
    if (rows.length == 1) {
        //动态加载
        var editWindow = $('<div/>').dialog(
            {
                width : 550,
                height : 520,
                href : '../lifeService/serviceTypeEdit.html',
                modal : true,
                title : '编辑服务类型信息',
                maximizable:true,
                cache:false,
                minimized:true,
                buttons : [{
                    text : '取&nbsp;&nbsp;消',
                    width : 60,
                    handler : function() {
                        editWindow.dialog('close');
                    }
                },
                    {
                        text : '保&nbsp;&nbsp;存',
                        width : 60,
                        handler : function() {
                            $('#editServiceType').form('submit', {
                                url:'/auth/lifeService/serviceType/edit',
                                onSubmit: function(){
                                    return $(this).form('validate')
                                },
                                success:function(data){
                                    data = eval('('+data+')');
                                    if(data.state == 'success'){
                                        editWindow.dialog('close');
                                        $('#serviceType').datagrid('load');
                                        showMsg('修改成功！');
                                    }else{
                                        showMsg('修改失败！');
                                    }
                                }
                            });
                        }
                    } ],
                onClose : function() {
                    $('#serviceType').datagrid('uncheckAll');// 取消选中的所有记录
                    $(this).dialog('destroy');//动态加载一定要销毁
                },
                //加载数据，用onLoad，不是用onOpen
                onLoad : function() {
                    $('#editServiceType').form('load', rows[0]);
                }
            });
    } else {
        $.messager.alert('提示', '请选择1条记录', 'error');
    }
}


function addst(){

        var editWindow = $('<div/>').dialog(
            {
                width : 550,
                height : 520,
                href : '../lifeService/serviceTypeAdd.html',
                modal : true,
                title : '添加服务类型信息',
                maximizable:true,
                cache:false,
                minimized:true,
                buttons : [{
                    text : '取&nbsp;&nbsp;消',
                    width : 60,
                    handler : function() {
                        editWindow.dialog('close');
                        //show();
                    }
                },
                    {
                        text : '保&nbsp;&nbsp;存',
                        width : 60,
                        handler : function() {
                            $('#editServiceType').form('submit', {
                                url:'/auth/lifeService/serviceType/add',
                                onSubmit: function(param){
                                    if($(this).form('validate')){
                                        if($('#serv_name').combobox('getValue')){
                                            alert('输入的内容已存在');
                                            return false;
                                        }else{
                                            param.serv_name = $('#serv_name').combobox('getText');
                                            return true;
                                        }

                                    }else{
                                        return false;
                                    }
                                },
                                success:function(data){
                                    data = eval('('+data+')');
                                    if(data.state == 'success'){
                                        editWindow.dialog('close');
                                        $('#serviceType').datagrid('load');
                                        showMsg('修改成功！');
                                    }else{
                                        showMsg('修改失败！');
                                    }
                                }
                            });
                        }
                    } ],
                onClose : function() {
                    $('#serviceType').datagrid('uncheckAll');// 取消选中的所有记录
                    $(this).dialog('destroy');//动态加载一定要销毁
                },
                //加载数据，用onLoad，不是用onOpen
                onLoad : function() {

                }
            });
}

function showMsg(msg){
    $.messager.show({
        title:'提示',
        msg:msg,
        timeout:2000,
        showType:'fade'
    });

}
